class Solution {
public:
    int dx[4]={0,0,1,-1};
    int dy[4]={1,-1,0,0};
    vector<vector<int>> highestPeak(vector<vector<int>>& i) {
        int m=i.size();
        int n=i[0].size();
        vector<vector<int>> v(m,vector<int>(n,-1));
        queue<pair<int,int>> q;
        for(int a=0;a<m;++a)
        {
            for(int b=0;b<n;++b)
            {
                if(i[a][b]==1) 
                {
                    v[a][b]=0;
                    q.push({a,b});
                }
            }
        }
        while(q.size())
        {
            int sz=q.size();
            while(sz--)
            {
                auto [x,y]=q.front();
                q.pop();
                for(int qi=0;qi<4;++qi)
                {
                    int a1=x+dx[qi];
                    int b1=y+dy[qi];
                    if(a1>=0&&a1<m&&b1>=0&&b1<n
                    &&v[a1][b1]==-1)
                    {
                        v[a1][b1]=v[x][y]+1;
                        q.push({a1,b1});
                    }
                }
            }
        }
        return v;
    }
};
